﻿using System.Reflection;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.Extensions.Logging;

namespace Microsoft.Extensions.DependencyInjection;

public static class EfCoreServiceCollectionExtensions
{
    /// <summary>
    /// 添加仓储设置
    /// </summary>
    /// <param name="services"></param>
    /// <param name="connectionString"></param>
    /// <returns></returns>
    public static IServiceCollection AddRepository(this IServiceCollection services, string connectionString)
    {
        services.AddDbContextPool<SimpleDbContext>(option =>option.UseOracle(connectionString), poolSize: 15);
        // 添加基于 ADO.NET 的 CustomDbContext
        services.AddScoped<ICurrentUserService, CurrentUserService>();
        services.AddScoped<CustomDbContext>(provider =>
        {
            var currentUserService = provider.GetRequiredService<ICurrentUserService>();
            return new CustomDbContext(connectionString);
        });
        //services.AddScoped<SimpleDbContext>();
        return services;
    }
}
